package listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext application = se.getSession().getServletContext();
        Integer online = (Integer) application.getAttribute("count");
        if(online==null){
            online=1;
        }else{
            online+=1;
        }
        application.setAttribute("count",online);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext application = se.getSession().getServletContext();
        Integer online = (Integer) application.getAttribute("count");
        if(online==null){
            online=0;
        }else{
            online-=1;
        }
        application.setAttribute("count",online);
    }
}
